package com.bnz.search.config;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import lombok.Data;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 配置elasticsearch
 */
@Data
@ConfigurationProperties(prefix = "elasticsearch")
@Configuration
public class ElasticsearchConfig {
    private String host;
    private int port;

    // 得到一个ElasticsearchClient对象，将这个对象放到spring容器中
    @Bean
    public ElasticsearchClient elasticsearchClient(){
        // 创建一个RestClient对象，传递主机和端口
        RestClient restClient = RestClient.builder(new HttpHost(host,port)).build();
        // 创建一个ElasticsearchTransport对象
        ElasticsearchTransport transport = new RestClientTransport(restClient,new JacksonJsonpMapper());
        // 返回ElasticsearchClient对象
        return new  ElasticsearchClient(transport);
    }
}
